package com.xmzs.sd.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.xmzs.common.core.domain.R;
import com.xmzs.common.core.validate.AddGroup;
import com.xmzs.common.core.validate.EditGroup;
import com.xmzs.common.mybatis.core.page.PageQuery;
import com.xmzs.common.mybatis.core.page.TableDataInfo;
import com.xmzs.common.web.core.BaseController;
import com.xmzs.sd.domain.bo.SysFileInfoBo;
import com.xmzs.sd.domain.vo.SysFileInfoVo;
import com.xmzs.sd.service.ISysFileInfoService;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 文件记录
 *
 * @author Lion Li
 * @date 2023-07-15
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/fileInfo")
public class SysFileInfoController extends BaseController {

    private final ISysFileInfoService sysFileInfoService;

    /**
     * 查询文件记录列表
     */
    @SaCheckPermission("system:fileInfo:list")
    @GetMapping("/list")
    public TableDataInfo<SysFileInfoVo> list(SysFileInfoBo bo, PageQuery pageQuery) {
        return sysFileInfoService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取文件记录详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:fileInfo:query")
    @GetMapping("/{id}")
    public R<SysFileInfoVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(sysFileInfoService.queryById(id));
    }

    /**
     * 新增文件记录
     */
    @SaCheckPermission("system:fileInfo:add")
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysFileInfoBo bo) {
        return toAjax(sysFileInfoService.insertByBo(bo));
    }

    /**
     * 修改文件记录
     */
    @SaCheckPermission("system:fileInfo:edit")
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysFileInfoBo bo) {
        return toAjax(sysFileInfoService.updateByBo(bo));
    }

    /**
     * 删除文件记录
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:fileInfo:remove")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(sysFileInfoService.deleteWithValidByIds(List.of(ids), true));
    }
}
